package persistence;

import persistence.db.SQLDatabase;
import persistence.javaserialize.SerialItemsDAO;
import persistence.javaserialize.SerialProductContainersDAO;
import persistence.javaserialize.SerialProductsDAO;

/**
 * @author Trevor Dixon
 * 
 *         Returns the appropriate data access objects for different persistence modes.
 */
public enum PersistenceMode
{
  JavaSerialize
  {
    @Override
    public ItemsDAO getItemsDAO()
    {
      // TODO cache it instead of creating a new one each time
      return new SerialItemsDAO();
    }

    @Override
    public ProductsDAO getProductsDAO()
    {
      // TODO cache it instead of creating a new one each time
      return new SerialProductsDAO();
    }

    @Override
    public ProductContainersDAO getProductContainersDAO()
    {
      // TODO cache it instead of creating a new one each time
      return new SerialProductContainersDAO();
    }
  },

  DB
  {
    private SQLDatabase db;

    @Override
    public ItemsDAO getItemsDAO()
    {
      if (db == null)
        db = new SQLDatabase();

      return db.getItems();
    }

    @Override
    public ProductsDAO getProductsDAO()
    {
      if (db == null)
        db = new SQLDatabase();

      return db.getProducts();
    }

    @Override
    public ProductContainersDAO getProductContainersDAO()
    {
      if (db == null)
        db = new SQLDatabase();

      return db.getProductContainers();
    }
  };

  /**
   * @return The items data access object for the given persistence mode.
   */
  public abstract ItemsDAO getItemsDAO();

  /**
   * @return The products data access object for the given persistence mode.
   */
  public abstract ProductsDAO getProductsDAO();

  /**
   * @return The product containers data access object for the given persistence mode.
   */
  public abstract ProductContainersDAO getProductContainersDAO();

  private static PersistenceMode systemMode = null;

  public static PersistenceMode getSystemMode()
  {
    return systemMode;
  }

  public static void setSystemMode(PersistenceMode _systemMode)
  {
    systemMode = _systemMode;
  }
}
